package com.cyq.day24.regex;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LoginFrame extends JFrame {
    private JPanel panel1,panel2,panel3,panel4,panel5;
    private JLabel label1,label2,label3,label4;
    private JTextField textField1,textField2,textField3,textField4;
    private JButton button1,button2;
    static ArrayList<User> arrayList=new ArrayList<>();
    public LoginFrame(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("登录界面");
        this.setBounds(300,100,500,500);
        this.setLayout(new GridLayout(5,1,5,5));
        panel1=new JPanel();
        label1=new JLabel("用户名");
        panel1.add(label1);
        textField1=new JTextField(20);
        panel1.add(textField1);
        panel2=new JPanel();
        label2=new JLabel("密码");
        panel2.add(label2);
        textField2=new JTextField(20);
        panel2.add(textField2);
        panel3=new JPanel();
        label3=new JLabel("邮箱");
        panel3.add(label3);
        textField3=new JTextField(20);
        panel3.add(textField3);
        panel5=new JPanel();
        label4=new JLabel("电话");
        panel5.add(label4);
        textField4=new JTextField(20);
        panel5.add(textField4);
        panel4=new JPanel();
        button1=new JButton("注册");
        button2=new JButton("取消");
        panel4.add(button1);
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean flag=false;
                String regex1="[A-Z][a-zA-Z0-9]{5,11}";
                Pattern pattern1 = Pattern.compile(regex1);
                Matcher matcher1=pattern1.matcher(textField1.getText());
                String regex2="[a-zA-Z]{6,12}";
                Pattern pattern2 = Pattern.compile(regex2);
                Matcher matcher2=pattern2.matcher(textField2.getText());
                String regex3="\\w+[@]\\w+[.]\\w+";
                Pattern pattern3 = Pattern.compile(regex3);
                Matcher matcher3=pattern3.matcher(textField3.getText());
                String regex4="1(3|5|7|8)\\d{9}";
                Pattern pattern4 = Pattern.compile(regex4);
                Matcher matcher4=pattern4.matcher(textField4.getText());
                if (!matcher1.matches()){
                    JOptionPane.showMessageDialog(null,"用户名格式错误");
                    return;
                }if (textField1.getText().equals("")||textField1.getText()==null){
                    JOptionPane.showMessageDialog(null,"用户名不能为空");
                    return;
                }
                if (!matcher2.matches()){
                    JOptionPane.showMessageDialog(null,"密码格式错误");
                    return;
                }
                if (textField2.getText().equals("")||textField2.getText()==null){
                    JOptionPane.showMessageDialog(null,"密码不能为空");
                    return;
                }
                if (!matcher3.matches()){
                    JOptionPane.showMessageDialog(null,"邮箱格式错误");
                    return;
                }
                if (!matcher4.matches()){
                    JOptionPane.showMessageDialog(null,"电话格式错误");
                    return;
                }if (textField4.getText().equals("")||textField4.getText()==null){
                    JOptionPane.showMessageDialog(null,"电话不能为空");
                    return;
                }
                flag=true;
                if (flag){
                    User user=new User(textField1.getText(),textField2.getText(),textField3.getText(),textField4.getText());
                    arrayList.add(user);

                }
            }
        });
        panel4.add(button2);
        this.add(panel1);
        this.add(panel2);
        this.add(panel5);
        this.add(panel3);
        this.add(panel4);
    }

    public static void main(String[] args) {
        init();
        LoginFrame loginFram=new LoginFrame();
        loginFram.setVisible(true);
        DetailFrame detailFrame=new DetailFrame(arrayList);
        detailFrame.setVisible(true);
    }

    private static void init() {
        User user1=new User("Asafaaa","sadfadsf","123@123.cc","13734526323");
        User user2=new User("Bsadaa","gasdfadsf","321@321.cc","13523461332");
        arrayList.add(user1);
        arrayList.add(user2);
    }


}
